package com.gopay.scheduler.job;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import com.gopay.common.datasource.DBType;
import com.gopay.common.datasource.DbContextHolder;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.cron.service.trans.ReportConsoleTransQueryService;
import com.gopay.cron.service.trans.TransQueryService;
import com.gopay.cron.web.vo.TransQueryResultVO;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.service.reporthandler.impl.UpdateReportConsoleTransQueryTask;

public class UpdateReportConsoleTransQueryJob extends GopayBaseJob {

    private final static int pageSize = 2000;
    private static final int MAX_THREAD_COUNT=10;
    private static final int size=pageSize/MAX_THREAD_COUNT;
    
    private ExecutorService threadPool = Executors.newFixedThreadPool(MAX_THREAD_COUNT);
    // 日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Resource(name = "reportConsoleTransQueryService")
    private ReportConsoleTransQueryService reportConsoleTransQueryService;

    @Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;
    
    @Resource(name = "transQueryService")
    private TransQueryService transQueryService;
    
    @Override
    public void doJobInternal() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,   -1);
        
        String ymd = new SimpleDateFormat( "yyyyMMdd ").format(cal.getTime());
        //TODO 测试数据
        // ymd="20160624";
        
        // 查询主表里清算时间为昨天的记录 分页查询
        int i = 1;
        while(true){
            List<TransQueryResultVO> list = transQueryService.getMainOrdersByOrderOrderStlmTime(ymd, i, pageSize );
            long iStartMillions = System.currentTimeMillis();
            logger.info("第" + i +"次查询 list.size=" + list.size());
            if(CollectionUtils.isEmpty( list)){
            	logger.info("执行结束，共执行" + i + "次");
                break;
            }
            
            // 更新或缓存表
            logger.info("开始第"+i+ "次更新缓存表");
           
            int count = (int) Math.ceil((list.size()*1.0)/(pageSize/MAX_THREAD_COUNT));
            CountDownLatch latch = new CountDownLatch(count);
            
            List<TransQueryResultVO> sublist = new ArrayList<TransQueryResultVO>();
            for(int j = 0; j < list.size(); j++){
            	sublist.add(list.get(j));
            	
            	if((j+1)%size == 0 || j == (list.size()-1)){
            		UpdateReportConsoleTransQueryTask t = new UpdateReportConsoleTransQueryTask(reportConsoleTransQueryService,sublist,latch);
                	threadPool.execute(t);
            		sublist= new ArrayList<TransQueryResultVO>();
            	}
            }

            long endMillions = System.currentTimeMillis();
            logger.info("第"+i+ "次更新缓存表结束 共耗费" +(( endMillions*1.0 - iStartMillions) / 1000) +"秒");
            i++;
        }
        
    }


}
